var sex = 0;//性别
var marry = 1;//婚否
var physical;//自我检测
var uid,ws,page,photo,takeP = [],times=1000,p_index,pic_index,task;
document.addEventListener('plusready',function(){
	uid = plus.storage.getItem('uid');//用户id
	var ws = plus.webview.currentWebview();
	var id = ws.aid;
	page = ws.page;
	console.log(id+" "+page);
	show_hide();
	var info = plus.storage.getItem('family');
	var ilist = $.parseJSON(info);
	$.each(ilist, function(k,v) {
		plus.nativeUI.closeWaiting();
		if(v.aid == id){
//			console.log(JSON.stringify(v));
			plus.nativeUI.closeWaiting();
			physical = v.physical;
			if(!physical){
				$('.ziwotest').find('span').hide();
			}
			$('.name').val(v.name);
			photo = v.photo;
			$('.uploadPic').attr('src',getAvatar(v.photo));
			$('.picinfo').attr('src',webRoot+v.thumb);
			if(v.sex == 1){
				$('.sex').attr('checked',true);
				sex = 1;
			}
			$('.age').val(v.age);  
			if(v.marry == 0){
				$('.marry').attr('checked',true);
				marry = 0;
			}
			$('.nation').val(v.nation);  
			$('.height').val(v.height);
			$('.weight').val(v.weight); 
			$('.phone').val(v.phone);
			$('.address').val(v.address);  
			if(v.caselist.length > 0){
				$('.wubl').hide();
				var str2 = '';
				$.each(v.caselist, function(key1,value1) {
					str2 += '<div class="mui-table-view-cell mui-table-view case-list" bid="'+value1.aid+'">'+
								'<li>'+
									'<span>'+value1.aidsname+'</span>'+
									'<span class="mui-h6 mui-pull-right">'+value1.ztime+'</span>'+
								'</li>'+
								'<li class="font-12 line-h-25">'+value1.history+'</li>'+
							'</div>';
				});
				$('.youbl').html(str2);
			}else{
				$('.wubl').show();
				$('.youbl').hide();
			}
		}
	});
	$("input[name='radio1']").click(function(){
	    sex= $(this).attr("value");//性别
    });
    $("input[name='radio2']").click(function(){
    	marry= $(this).attr("value");//婚否
    });
	uid = plus.storage.getItem('uid');//用户id
	//添加病例
	$('.bingli').click(function(){
		goNewPage('me-archives-home-add-case.html',{spage:ws.id, fid:id})
	})
	//选择拍照添加
	$('.paizhao2').on('tap', function(){
		var cmr = plus.camera.getCamera(); 
		cmr.captureImage( function(path){
			$('.uploadPic').attr('src',plus.io.convertLocalFileSystemURL(path));
			appendonePic(path);
		}, function(err){}, {index:1} );
	})
	//选择相册添加
	$('.photo2').on('tap', function(){
		plus.gallery.pick( function(path){
		 	$('.uploadPic').attr('src',path);
		 	appendonePic(path);
		}, function(error){}, {} );
	})
    var index = 1;
	var files = [];
	// 添加照片
	function appendonePic(p) {
		files.push({name:"uploadkey"+index,path:p});
		index++;
		upload();
	}
	//创建对像
	function upload(){ 
		showWating();
		var  task = plus.uploader.createUpload(apiRoot+'?m=Home&c=Member&a=uploadOne&id='+uid,
		{ method:"POST"},
		function ( t, status ) {  
			// 上传完成     
			if ( status == 200 ) { 
				console.log(t.responseText);
				plus.nativeUI.toast("上传成功");    
				$(".uploadPic").attr('src',webRoot+t.responseText);
				photo = t.responseText;
				plus.nativeUI.closeWaiting();
			} else {
				plus.nativeUI.toast( "上传失败："+status );
				plus.nativeUI.closeWaiting();
			}  
		});
//		console.log(JSON.stringify(task));
		for(var i=0;i<files.length;i++){
			var f=files[i];
			task.addFile(f.path,{key:f.name});
		}
		task.start();
	}
	//自我检测
	$('.ziwotest').click(function(){
		console.log(physical);
		if(physical){//已有自我检测结果，直接显示
			goNewPage('me-family-inf.html',{physical:physical,spage:1})
		}else{
			goNewPage('tzjc-home-list.html',{spage:ws.id})
		}
	})
	var server1;
	//点击完成-》修改家庭成员信息
	$('.wancheng').click(function(){
		if($(this).text() == '操作'){
			$('input, textarea').attr('disabled', false);
			$('.bingli').show();
			$('.ziwotest').find('span').show();
			$('img').addClass('pic');
			//上传照片-》单图
		    $('.pic').on('click', function() {
				mui('#sheet2').popover('toggle');
			})
			$(this).text('完成');
		}else{
			var name = $('.name').val();
	    	var age = $('.age').val();
	    	var nation = $('.nation').val();
	    	var height = $('.height').val();
	    	var weight = $('.weight').val();
	    	var phone = $('.phone').val();
	    	var address = $('.address').val();
	    	
	    	var bid = ',';
	    	$('.case-list').each(function(){
	    		bid += $(this).attr('bid') + ",";
	    	})
	    	if(bid == ',')bid='';
	    	console.log(age+" "+nation+" "+phone+" "+address+" "+sex+" "+marry+" "+height+" "+weight+" "+bid+" "+physical);
	    	if(!age.match(a) || !nation.trim() || !phone.trim() || !address.trim() || !height.match(h) || !weight.match(w)){
	    		toast('请补全信息并检查是否正确');return;
	    	}
	    	if(!phone.match(p1)){
	    		toast('手机号格式错误');
	    	}
	    	showWating();
			$.ajax({
				type:"get",
				url:apiRoot+"?m=Home&c=member&a=saveFamily",
				data : {
					aid :id,
					name :name,
					age :age,
					nation :nation,
					phone :phone,
					address : address,
					sex :sex,
					marry :marry,
					height :height,
					weight :weight,
					photo :photo,
					cases :bid,
					physical : physical
				},
				success:function(data){
					console.log(data);
					if(data > 0){
						if(page == 'me-family-search-list.html'){
							if(page)plus.webview.getWebviewById(page).close();
							plus.webview.getWebviewById('me-family-list.html').reload();
						}else{
							if(page)plus.webview.getWebviewById(page).reload();
						}
						toast('修改成功');
						plus.webview.currentWebview().close();
					}
					plus.nativeUI.closeWaiting();  
					$('.wancheng').text('操作');
					if(!physical){
						$('.ziwotest').find('span').hide();
					}
					show_hide();
				},
				error:function(e){
					console.log(JSON.stringify(e));
					errortoast();
				}
			});
		}
	})
	//进入病例详情
	$('.case-list').on('click',function(){
		goNewPage('me-archives-home-add-case.html',{aid:$(this).attr('bid')})
	})
},false)
function show_hide(){
	$('input, textarea').attr('disabled', true);
	$('.bingli').hide();
	$('img').removeClass('pic');
	$('img').unbind('click');
}
window.addEventListener('change',function(event){
	var bid = event.detail.bid;
	var aidsname = event.detail.aidsname;
	var ztime = event.detail.ztime;
	var bing = event.detail.bing;
	console.log(bid+"," +aidsname+', '+ztime+', '+bing);
	$('.wubl').hide();
	$('.youbl').show();
	var list = '<div class="mui-table-view-cell mui-table-view case-list" bid="'+bid+'">'+
					'<li>'+
						'<span>'+aidsname+'</span>'+
						'<span class="mui-h6 mui-pull-right">'+ztime+'</span>'+
					'</li>'+
					'<li class="font-12 line-h-25">'+bing+'</li>'+
				'</div>';
	$('.youbl').append(list);
	//进入病例详情
	$('.case-list').on('click',function(){
		goNewPage('me-archives-home-add-case.html',{aid:$(this).attr('bid')})
	})
})
window.addEventListener('ziwotest',function(event){
	physical = event.detail.physical;
	console.log(physical);
})